package uit.mmt02.activity;

import java.util.ArrayList;
import java.util.List;

import uit.mmt02.R;
import uit.mmt02.share.DB_EmailLoginManager_Adapter;
import uit.mmt02.share.EmailLoginManager;
import uit.mmt02.share.ShowEmailLoginAdapter;
import uit.mmt02.share.showToast;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
//class Hien Thi DS cac Email da Login vao chuong trinh, tu dong luu lai
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class ListMailActivity extends Activity 
{
	final static String KEY_LIST_USERNAME = "listUsername";
	final static String KEY_LIST_PASSWORD = "listPassword";
	final static String KEY_USERNAME_LOGIN = "USERNAMELOGIN"; // GUI USERVA PASS CHO BEN MAIL CONTROL LOGIN
	final static String KEY_PASSWORD_LOGIN = "PASSWORDLOGIN";
	List<EmailLoginManager>list;
	ArrayAdapter<EmailLoginManager>adapter;
	DB_EmailLoginManager_Adapter mydb;
	String tag = "ListMailActivity";
	showToast st;
	ListView listview;
//	TextView txtusername;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_mail_activity);
		st = new showToast(this);
		listview = (ListView)findViewById(R.id.listview_listmail);
		//mo database lau DU lieu ve
		mydb = new DB_EmailLoginManager_Adapter(getBaseContext());
		mydb.open();
		Cursor mCursor = mydb.get_email_tongmail_newmail();
		list = new ArrayList<EmailLoginManager>();
		list = mydb.convertCursorToList(mCursor);
		mCursor.close();
		mydb.close();
		adapter = new ShowEmailLoginAdapter(this, list);
		listview.setAdapter(adapter);
	}
	
	//su kien click 2 button duyet mail va new mail
	public void btn_onClick(View v) {
		// TODO Auto-generated method stub
		Button btn = (Button)v;
		switch (btn.getId()) {
		case R.id.btn_listmail_duyet:
//			adapter.notifyDataSetChanged();
//			st.showmess("duyt mail");
			//gui nhung mail dc chon qua show mail activity
			Intent intentShowmail = new Intent(this,  uit.mmt02.activity.showMailActivity.class);
			ArrayList<String>listUsername = getListUsername();
			ArrayList<String>listpassword = getLisPassword();
			
			Bundle bundle = new Bundle();
			bundle.putStringArrayList(KEY_LIST_USERNAME,listUsername);
			bundle.putStringArrayList(KEY_LIST_PASSWORD, listpassword);
//			Log.i(tag, "set bundle");
			intentShowmail.putExtras(bundle);
			startActivity(intentShowmail);
			Log.i(tag, "start new intent");
			break;
		case R.id.btn_listmail_newmail:
			startActivity(new Intent(this, uit.mmt02.activity.loginActivity.class));
			break;

		default:
			break;
		}

	}
	
	//thiet lap menu
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.listmail_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}
	//click item
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.item_newEmaillogin:
			startActivity(new Intent(this, uit.mmt02.activity.loginActivity.class));
			break;
		case R.id.item_duyetmail:
			adapter.notifyDataSetChanged();
//			st.showmess("duyt mail");
			//gui nhung mail dc chon qua show mail activity
			Intent intentShowmail = new Intent(this,  uit.mmt02.activity.showMailActivity.class);
			ArrayList<String>listUsername = getListUsername();
			ArrayList<String>listpassword = getLisPassword();
			
			Bundle bundle = new Bundle();
			bundle.putStringArrayList(KEY_LIST_USERNAME,listUsername);
			bundle.putStringArrayList(KEY_LIST_PASSWORD, listpassword);
//			Log.i(tag, "set bundle");
			intentShowmail.putExtras(bundle);
			startActivity(intentShowmail);
			Log.i(tag, "start new intent");
			break;
		case R.id.item_removeEmailLogin:
//			mydb.open();
//			mydb.remove_by_username("vipboy286");
//			mydb.close();
			int rows = remove_checked();
			Log.i("remove login email", String.valueOf(rows));
			startActivity(new Intent(this,  uit.mmt02.activity.ListMailActivity.class));
			break;
		case R.id.item_mark:
			all_check();
			break;
		case R.id.item_unmark:
			all_Uncheck();
			break;
		case R.id.item_autoCSDL:
			mydb.open();
			mydb.autoCSDL();
			mydb.close();
			startActivity(new Intent(this,  uit.mmt02.activity.ListMailActivity.class));
			break;
		case R.id.item_deleteALL_listmail:
			mydb.open();
			mydb.removeALL();
			mydb.close();
			startActivity(new Intent(this,  uit.mmt02.Esafe2Activity.class));
			break;
		case R.id.item_menu_listLogin_test1:
			Bundle bun = new Bundle();
			bun.putString(KEY_USERNAME_LOGIN, "thaithao07520323@gmail.com");
			bun.putString(KEY_PASSWORD_LOGIN,"abc@123456");
			Intent i2 = new Intent(this, getGmailActivity.class);
			i2.putExtras(bun);
			Log.v("gui username", "va password");
			startActivity(i2);
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	private void all_check() //check tat ca cac checkbox
	{
		int n_rows = list.size();
		Log.i(tag, String.valueOf(n_rows));
		for(int i = 0; i < n_rows; i ++)
		{
			list.get(i).setChecked(true);
		}
		adapter.notifyDataSetChanged();
	}
	private void all_Uncheck() //un check tat ca cac checkbox
	{ 
		int n_rows = list.size();
		Log.i(tag, String.valueOf(n_rows));
		for(int i = 0; i < n_rows; i ++)
		{
			list.get(i).setChecked(false);
		}
		adapter.notifyDataSetChanged();
	}
	
	private int remove_checked()
	{
		int row_remove = 0;
		mydb.open();
		for (int i = 0; i < list.size(); i ++)
		{
			if(list.get(i).isChecked())
			{
				mydb.remove_by_username(list.get(i).getUsername());
				row_remove ++;
			}
		}
		mydb.close();
		return row_remove;
	}
	
	private ArrayList<String> getListUsername()
	{
		ArrayList<String> alist = new ArrayList<String>();
		for (int i = 0; i < list.size(); i ++)
		{
			if(list.get(i).isChecked())
			{
				String u1 = list.get(i).getUsername();
				alist.add(u1);
				Log.i(tag, "add: " + u1);
			}
		}
		return alist;
	}
	
	private ArrayList<String> getLisPassword()
	{
		ArrayList<String> alist = new ArrayList<String>();
		for (int i = 0; i < list.size(); i ++)
		{
			if(list.get(i).isChecked())
			{
				String u1 = list.get(i).getPassword();
				alist.add(u1);
				Log.i(tag, "add: " + u1);
			}
		}
		return alist;
	}
	
	

}
